package com.puchi.sdkdemo.app.game.model;

import android.app.Application;
import android.graphics.Point
import android.text.TextUtils
import android.view.View
import com.google.gson.Gson
import com.orhanobut.logger.Logger
import com.zalyyh.mvvm.base.BaseViewModel;
import com.puchi.sdkdemo.app.game.activity.GamesActivity;
import com.puchi.sdkdemo.databinding.ActivityGamesBinding;
import com.puchi.sdkdemo.enty.H5enty
import com.puchi.sdkdemo.utils.AllUtlis
import com.zalyyh.advertisement.enty.AdaveData
import com.zalyyh.advertisement.interfac.AdverCallback
import com.zalyyh.advertisement.open.Adavert
import com.zalyyh.game.interfac.GameCallback
import com.zalyyh.mvvm.binding.data.command.BindingAction
import com.zalyyh.mvvm.binding.data.command.BindingCommand
import androidx.databinding.ObservableBoolean
import com.bytedance.applog.AppLog
import com.puchi.sdkdemo.JSBridge
import com.umeng.analytics.MobclickAgent

import layaair.game.browser.ExportJavaFunction


class GamesViewModel(application: Application) : BaseViewModel(application) {
    var yThis: GamesViewModel = this
    var activity: GamesActivity? = null
    var binding: ActivityGamesBinding? = null
    var mapH5Callback = hashMapOf<Int, String>()
    var rewardA: Adavert? = null
    var screenVideoA: Adavert? = null
    var nativeExpressA: Adavert? = null
    var ggShow = false

    var isShow = ObservableBoolean(false)
    var click = BindingCommand<String>(BindingAction {
        isShow.set(false)
    })


    var xxValue = ""
    var xxShow = false
    var callback: GameCallback = object : GameCallback() {
        override fun interactive(key: Int, type: Int, value: String, keys: String) {
            if (activity == null || activity!!.isFinishing) return
            //Logger.e("zalyyh --GamesViewModel   key: $key    type:  $type    value:  $value    keys:  $keys")
            if (!xxShow && !TextUtils.isEmpty(xxValue)) {
                showBanner(1, xxValue)
            }
            if (key == 1) {  //打点 处理关卡


            } else if (key == 2) {
                showBanner(type, value)
                xxValue = value
                Thread(Runnable {
                    Thread.sleep(10000)
                    showBanner(type, value)
                }).start()
            } else if (key == 3) {    /*打开插屏广告*/

            } else if (key == 4) {    /*打开激励视频*/
                showRewardVideoAd(value)
//                loadFullScreenVideoAd(value)
            } else if (key == 5) {   /*打开信息流广告*/
                ggShow = true
                loadNativeExpressAd(value)
            } else if (key == 6) {
                binding!!.news.visibility = View.GONE
                ggShow = false
                nativeExpressA!!.preload(null, 3)
            } else if (key == 7) {
                mapH5Callback.put(type, value)
            } else if (key == 9) {
                loadFullScreenVideoAd(value)
            }
        }
    };

    fun setData(a: GamesActivity, b: ActivityGamesBinding) {
        activity = a;
        binding = b;
        nativeExpressA = Adavert(activity!!, object : AdverCallback() {
            override fun show(data: AdaveData) {
                if (ggShow) {
                    binding!!.news.visibility = View.VISIBLE
                    binding!!.news!!.removeAllViews()
                    binding!!.news!!.addView(data.view)
                    var map = hashMapOf<String, String>()
                    map.put("userid", AppLog.getDid())
                    MobclickAgent.onEvent(activity, "showExpressAd", map);
                }
            }
        })
        rewardA = Adavert(activity!!, object : AdverCallback() {
            override fun show(data: AdaveData) {
                data.showRewardVideoAd(activity!!)
            }

            override fun onAdShow() {
                var map = hashMapOf<String, String>()
                map.put("userid", AppLog.getDid())
                MobclickAgent.onEvent(activity, "showAwardVideo", map);
                ExportJavaFunction.CallBackToJS(
                    JSBridge::class.java, "showRewardVideoAd", "{" +
                            "\"code\": 100," +
                            "\"message\": \"视频开始播放\"" +
                            "}"
                )
            }

            override fun onVideoError() {
                ExportJavaFunction.CallBackToJS(
                    JSBridge::class.java, "showRewardVideoAd", "{" +
                            "\"code\": -1," +
                            "\"message\": \"视频播放失败\"" +
                            "}"
                )
            }

            //视频广告关闭回调
            override fun onAdClose() {
                ExportJavaFunction.CallBackToJS(
                    JSBridge::class.java, "showRewardVideoAd", "{" +
                            "\"code\": 0," +
                            "\"message\": \"视频关闭\"" +
                            "}"
                )
            }

            //视频广告播放完毕回调
            override fun onVideoComplete() {
                ExportJavaFunction.CallBackToJS(
                    JSBridge::class.java, "showRewardVideoAd", "{" +
                            "\"code\": 200," +
                            "\"message\": \"视频播放完毕\"" +
                            "}"
                )
            }

            //视频广告播完验证奖励有效性回调，参数分别为是否有效，奖励数量，奖励名称
            override fun onRewardVerify(var1: Boolean, var2: Int, var3: String) {
                Logger.d("是否发放奖励：$var1  奖励数量：$var2  奖励名称： $var3")
            }


        })
        screenVideoA = Adavert(activity!!, object : AdverCallback() {
            override fun show(data: AdaveData) {
                data.showFullScreenVideoAd(activity!!)
            }

            override fun onAdShow() {
                var map = hashMapOf<String, String>()
                map.put("userid", AppLog.getDid())
                MobclickAgent.onEvent(activity, "showFullScreenVideo", map);
                ExportJavaFunction.CallBackToJS(
                    JSBridge::class.java, "loadFullScreenVideoAd", "{" +
                            "\"code\": 100," +
                            "\"message\": \"视频开始播放\"" +
                            "}"
                )
                logString(
                    "{" +
                            "\"code\": 100," +
                            "\"message\": \"视频开始播放\"" +
                            "}"
                )
            }


            //视频广告关闭回调
            override fun onAdClose() {
                ExportJavaFunction.CallBackToJS(
                    JSBridge::class.java, "loadFullScreenVideoAd", "{" +
                            "\"code\": 0," +
                            "\"message\": \"视频关闭\"" +
                            "}"
                )

                logString(
                    "{" +
                            "\"code\": 0," +
                            "\"message\": \"视频关闭\"" +
                            "}"
                )
            }

            //视频广告播放完毕回调
            override fun onVideoComplete() {
                ExportJavaFunction.CallBackToJS(
                    JSBridge::class.java, "loadFullScreenVideoAd", "{" +
                            "\"code\": 200," +
                            "\"message\": \"视频播放完毕\"" +
                            "}"
                )
                logString(
                    "{" +
                            "\"code\": 200," +
                            "\"message\": \"视频播放完毕\"" +
                            "}"
                )
            }

            /**
             * 跳过视频播放
             */
            override fun onSkippedVideo() {
                ExportJavaFunction.CallBackToJS(
                    JSBridge::class.java, "loadFullScreenVideoAd", "{" +
                            "\"code\": 201," +
                            "\"message\": \"跳过视频播放\"" +
                            "}"
                )
                logString(
                    "{" +
                            "\"code\": 201," +
                            "\"message\": \"跳过视频播放\"" +
                            "}"
                )
            }


        })
    }

    var butt = BindingCommand<String>(BindingAction {
        /*var s = "{\t\"video_title\": \"Lady Gaga on Oscar\",\t\"duration\": 20}"
        JSBridge.appLog_dot("cesshi",s)
        JSBridge.getAppConfigure(0)
        JSBridge.loadBannerExpressAd(0.0,"{\t\"w\": 300,\t\"codeld\": \"Lady Gaga on Oscar\",\t\"h\": 300}")
        JSBridge.showRewardVideoAd(0.0,"{\t\"w\": 300,\t\"number\": 300,\t\"codeld\": \"Lady Gaga on Oscar\",\t\"h\": 300}")
        JSBridge.showInteractionExpressAd("{\t\"w\": 300,\t\"codeld\": \"Lady Gaga on Oscar\",\t\"h\": 300}")
        JSBridge.loadNativeExpressAd("{\t\"w\": 300,\t\"codeld\": \"Lady Gaga on Oscar\",\t\"h\": 300}")*/
    })


    fun showBanner(type: Int, value: String) {
        var json = Gson().fromJson<H5enty>(value, H5enty::class.java)

        var bannerA = Adavert(activity!!, object : AdverCallback() {
            override fun show(data: AdaveData) {
                binding!!.banner!!.removeAllViews()
                binding!!.banner!!.addView(data.view)
                var map = hashMapOf<String, String>()
                map.put("userid", AppLog.getDid())
                MobclickAgent.onEvent(activity, "showBannerAd", map);
                xxShow = true
            }
        })
        if (json.preloading == 1) {
            bannerA!!.preload(
                bannerA!!.setAdSlot(
                    json.codeId,
                    json.w.toFloat(),
                    json.h.toFloat()
                ).build(), 0
            )
        } else
            bannerA!!.show(
                bannerA!!.setAdSlot(
                    json.codeId,
                    json.w.toFloat(),
                    0f
                ).build(), 0
            )
    }

    fun showRewardVideoAd(value: String) {
        var json = Gson().fromJson<H5enty>(value, H5enty::class.java)
        if (json.preloading == 1) {
            rewardA!!.preload(
                rewardA!!.setAdSlot(
                    json.codeId,
                    json.w.toFloat(),
                    json.h.toFloat()
                ).build(), 2
            )
        } else
            rewardA!!.show(
                rewardA!!.setAdSlot(
                    json.codeId,
                    json.w.toFloat(),
                    json.h.toFloat()
                ).build(), 2
            )
    }

    fun loadFullScreenVideoAd(value: String) {
        var json = Gson().fromJson<H5enty>(value, H5enty::class.java)
        json.w = get().x.toFloat()
        json.h = get().y.toFloat()
        json.codeId = "942340521"
        if (json.preloading == 1) {
            screenVideoA!!.preload(
                screenVideoA!!.setAdSlot(
                    json.codeId,
                    json.w.toFloat(),
                    json.h.toFloat()
                ).build(), 4
            )
        } else
            screenVideoA!!.show(
                screenVideoA!!.setAdSlot(
                    json.codeId,
                    json.w.toFloat(),
                    json.h.toFloat()
                ).build(), 4
            )
    }

    fun loadNativeExpressAd(value: String) {
        Logger.e("zalyyh", "显示 $value")
        var json = Gson().fromJson<H5enty>(value, H5enty::class.java)
        val outSize = Point()
        activity!!.getWindowManager().getDefaultDisplay().getRealSize(outSize)
        var a = H5enty()
        a.w = outSize.x.toFloat()
        a.h = outSize.y.toFloat()
        var w = AllUtlis.px2dp(json.w)
        var h = AllUtlis.px2dp(a.h - json.top + 20)
        if (json.preloading == 1) {
            rewardA!!.preload(
                nativeExpressA!!.setAdSlot(
                    json.codeId,
                    w.toFloat(),
                    h.toFloat()
                ).build(), 3
            )
        } else
            nativeExpressA!!.show(
                nativeExpressA!!.setAdSlot(
                    json.codeId,
                    w.toFloat(),
                    h.toFloat()
                ).build(), 3
            )
    }


    fun logString(value: String) {
        //Logger.d("zalyyh1111"+value)
    }

    fun get(): Point {
        val outSize = Point()
        activity!!.getWindowManager().getDefaultDisplay().getRealSize(outSize)
        return outSize
    }

}